Interface


In [1]:
i=False
while not i:
    a = 1


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-1-86236fc5d22b> in <module>()
      1 i=False
      2 while not i:
----> 3     a = 1

KeyboardInterrupt: 

Basic usage


In [2]:
import numpy as np

In [3]:
a = 1
print a


1

In [4]:
print a


1

In [5]:
a = a + 1

In [6]:
print a


2

In [7]:
def myfunc(a):
    x = a + 1
    return x

myfunc(5)


Out[7]:
6

Plotting


In [8]:
%matplotlib inline

In [9]:
import seaborn as sns
sns.set(style="ticks")

# Load the example tips dataset
tips = sns.load_dataset("tips")

# Draw a nested boxplot to show bills by day and sex
ax = sns.boxplot(x="day", y="total_bill", hue="sex", data=tips, palette="PRGn")
sns.despine(offset=10, trim=True)



In [10]:
ax.get_figure().savefig('test.pdf')

Tables


In [11]:
import pandas as pd
mat = pd.read_csv('test.csv')
mat


Out[11]:
Unnamed: 0 total_bill tip sex smoker day time size
0 0 16.99 1.01 Female No Sun Dinner 2
1 1 10.34 1.66 Male No Sun Dinner 3
2 2 21.01 3.50 Male No Sun Dinner 3
3 3 23.68 3.31 Male No Sun Dinner 2
4 4 24.59 3.61 Female No Sun Dinner 4
5 5 25.29 4.71 Male No Sun Dinner 4
6 6 8.77 2.00 Male No Sun Dinner 2
7 7 26.88 3.12 Male No Sun Dinner 4
8 8 15.04 1.96 Male No Sun Dinner 2
9 9 14.78 3.23 Male No Sun Dinner 2
10 10 10.27 1.71 Male No Sun Dinner 2
11 11 35.26 5.00 Female No Sun Dinner 4
12 12 15.42 1.57 Male No Sun Dinner 2
13 13 18.43 3.00 Male No Sun Dinner 4
14 14 14.83 3.02 Female No Sun Dinner 2
15 15 21.58 3.92 Male No Sun Dinner 2
16 16 10.33 1.67 Female No Sun Dinner 3
17 17 16.29 3.71 Male No Sun Dinner 3
18 18 16.97 3.50 Female No Sun Dinner 3
19 19 20.65 3.35 Male No Sat Dinner 3
20 20 17.92 4.08 Male No Sat Dinner 2
21 21 20.29 2.75 Female No Sat Dinner 2
22 22 15.77 2.23 Female No Sat Dinner 2
23 23 39.42 7.58 Male No Sat Dinner 4
24 24 19.82 3.18 Male No Sat Dinner 2
25 25 17.81 2.34 Male No Sat Dinner 4
26 26 13.37 2.00 Male No Sat Dinner 2
27 27 12.69 2.00 Male No Sat Dinner 2
28 28 21.70 4.30 Male No Sat Dinner 2
29 29 19.65 3.00 Female No Sat Dinner 2
... ... ... ... ... ... ... ... ...
214 214 28.17 6.50 Female Yes Sat Dinner 3
215 215 12.90 1.10 Female Yes Sat Dinner 2
216 216 28.15 3.00 Male Yes Sat Dinner 5
217 217 11.59 1.50 Male Yes Sat Dinner 2
218 218 7.74 1.44 Male Yes Sat Dinner 2
219 219 30.14 3.09 Female Yes Sat Dinner 4
220 220 12.16 2.20 Male Yes Fri Lunch 2
221 221 13.42 3.48 Female Yes Fri Lunch 2
222 222 8.58 1.92 Male Yes Fri Lunch 1
223 223 15.98 3.00 Female No Fri Lunch 3
224 224 13.42 1.58 Male Yes Fri Lunch 2
225 225 16.27 2.50 Female Yes Fri Lunch 2
226 226 10.09 2.00 Female Yes Fri Lunch 2
227 227 20.45 3.00 Male No Sat Dinner 4
228 228 13.28 2.72 Male No Sat Dinner 2
229 229 22.12 2.88 Female Yes Sat Dinner 2
230 230 24.01 2.00 Male Yes Sat Dinner 4
231 231 15.69 3.00 Male Yes Sat Dinner 3
232 232 11.61 3.39 Male No Sat Dinner 2
233 233 10.77 1.47 Male No Sat Dinner 2
234 234 15.53 3.00 Male Yes Sat Dinner 2
235 235 10.07 1.25 Male No Sat Dinner 2
236 236 12.60 1.00 Male Yes Sat Dinner 2
237 237 32.83 1.17 Male Yes Sat Dinner 2
238 238 35.83 4.67 Female No Sat Dinner 3
239 239 29.03 5.92 Male No Sat Dinner 3
240 240 27.18 2.00 Female Yes Sat Dinner 2
241 241 22.67 2.00 Male Yes Sat Dinner 2
242 242 17.82 1.75 Male No Sat Dinner 2
243 243 18.78 3.00 Female No Thur Dinner 2

244 rows × 8 columns

Run system command


In [12]:
callback = ! ls -lrt
print callback


['total 176', '-rw-r--r--  1 haoyangz  staff   8810 Feb  9 22:20 test.csv', 'drwxr-xr-x  3 haoyangz  staff    102 Feb  9 22:34 I am a folder', 'drwxr-xr-x  6 haoyangz  staff    204 Feb  9 22:48 MNIST_data', '-rw-r--r--  1 haoyangz  staff  59012 Feb 10 16:35 Jupyter tutorial.ipynb', '-rw-r--r--  1 haoyangz  staff  14040 Feb 10 17:41 test.pdf']

In [13]:
cmd = 'ls {}'.format('-lrt')  ## The shell command you wish to execute
callback = ! $cmd
print callback


['total 176', '-rw-r--r--  1 haoyangz  staff   8810 Feb  9 22:20 test.csv', 'drwxr-xr-x  3 haoyangz  staff    102 Feb  9 22:34 I am a folder', 'drwxr-xr-x  6 haoyangz  staff    204 Feb  9 22:48 MNIST_data', '-rw-r--r--  1 haoyangz  staff  59012 Feb 10 16:35 Jupyter tutorial.ipynb', '-rw-r--r--  1 haoyangz  staff  14040 Feb 10 17:41 test.pdf']

Use markdown to document and organize the cells

Headline

Smaller

Even smaller

Text text text

  • List1
  • List2

A code block:

cd pset1
python run.py arg1 arg2

Tensorflow example


In [14]:
import tensorflow as tf

In [15]:
# Load MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)


Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz

In [16]:
# Define the input as a "placeholder", as we will feed the value in later
x = tf.placeholder(tf.float32, [None, 784])

In [17]:
# Define the parameters in the network as "Variable", which the model will learn
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

In [18]:
# Define the output y as a symbolic variable, the value of which depends on x, W and b 
y = tf.nn.softmax(tf.matmul(x, W) + b)

In [19]:
# Define the true label as "placeholder", as we will feed the value in later
y_true = tf.placeholder(tf.float32, [None, 10])

In [20]:
# Define the loss
# tf.reduce_sum: Computes the sum of elements across dimensions of a tensor.
# tf.reduce_mean Computes the mean of elements across dimensions of a tensor.
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_true * tf.log(y), reduction_indices=[1]))

In [21]:
# Define the task (optimize cross_entropy)
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

In [22]:
# Get the handle to initialization function
init = tf.initialize_all_variables()


WARNING:tensorflow:From <ipython-input-22-7ac142b6ba36>:2 in <module>.: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.

In [23]:
# Define a "session" and run initialization
sess = tf.Session()
sess.run(init)

In [24]:
# Use this session to train on 1000 mini-baches of size 100
for i in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  sess.run(train_step, feed_dict={x: batch_xs, y_true: batch_ys})

In [25]:
# Define symbolic variables that calculates accuracy 
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_true,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

In [26]:
# Run the session with a test image and its correct label provided to fetch the accuracy in predicting it's label
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_true: mnist.test.labels}))


0.9166

In [ ]: